from datetime import datetime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Table, Column, Integer, String, Text, DateTime, MetaData, ForeignKey, Boolean
from sqlalchemy.orm.attributes import InstrumentedAttribute

Base = declarative_base()

class Ca(Base):
    __tablename__ = 'cas'

    id = Column(Integer, primary_key = True)
    hashcode = Column(String(255))
    location = Column(String(255))
    dn = Column(String(255))
    valid_from = Column(DateTime)
    valid_to = Column(DateTime)

class Cert(Base):
    __tablename__ = 'certs'

    id = Column(Integer, primary_key = True)
    dn = Column(String(255))
    ca_id = Column(Integer, ForeignKey('cas.id'))
    public_key = Column(Text)
    private_key = Column(Text)

class Community(Base):
    __tablename__ = 'communities'

    id = Column(Integer, primary_key = True)
    name = Column(String(255))
    description = Column(String(255))
    ca_id = Column(Integer, ForeignKey('cas.id'))
    owner_id = Column(Integer)
    admin_role_id = Column(Integer)
    user_role_id = Column(Integer)

class Job(Base):
    __tablename__ = 'jobs'

    id = Column(Integer, primary_key = True)
    uuid = Column(String(255))
    user_id = Column(Integer, ForeignKey('users.id'))
    title = Column(String(255))
    description = Column(String(255))
    job_type = Column(String(255))
    content = Column(Text)
    status = Column(String(255))
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    job_info = Column(Text)

class Nic(Base):
    __tablename__ = 'nics'

    id = Column(Integer, primary_key = True)
    address = Column(String(255))
    netmask = Column(String(255))
    vswitch_id = Column(Integer, ForeignKey('vswitches.id'))
    virtual_machine_instance_id = Column(Integer, ForeignKey('virtual_machine_instances.id'))

class Role(Base):
    __tablename__ = 'roles'

    id = Column(Integer, primary_key = True)
    name = Column(String(255))

class Session(Base):
    __tablename__ = 'sessions'

    id = Column(Integer, primary_key = True)
    session_id = Column(String(255))
    data = Column(Text)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

class Software(Base):
    __tablename__ = 'softwares'

    id = Column(Integer, primary_key = True)
    name = Column(String(255))
    category = Column(String(255))
    downloadURL = Column(String(255))
    description = Column(String(255))

class Subscription(Base):
    __tablename__ = 'subscriptions'

    id = Column(Integer, primary_key = True)
    software_id = Column(Integer, ForeignKey('softwares.id'))
    user_id = Column(Integer, ForeignKey('users.id'))

class Task(Base):
    __tablename__ = 'tasks'

    id = Column(Integer, primary_key = True)
    uuid = Column(String(255))
    job_id = Column(Integer, ForeignKey('jobs.id'))
    title = Column(String(255))
    description = Column(String(255))
    task_type = Column(String(255))
    content = Column(Text)
    status = Column(String(255))
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    depend_task_id = Column(Integer)
    task_info = Column(Text)

class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key = True)
    email = Column(String(255))
    password = Column(String(255))
    cert_id = Column(Integer, ForeignKey('certs.id'))

class VclusterTemp(Base):
    __tablename__ = 'vcluster_temps'

    id = Column(Integer, primary_key = True)
    name = Column(String(255))
    description = Column(String(255))
    node_num = Column(Integer)
    user_id = Column(Integer, ForeignKey('users.id'))
    created_at = Column(DateTime, default=datetime.utcnow)
    capabilities = Column(String(255))
    sharedstorage_size = Column(Integer)
    sharedstorage_local = Column(String(255))
    uuid = Column(String(255))

class VclusterTempsVmTemp(Base):
    __tablename__ = 'vcluster_temps_vm_temps'

    id = Column(Integer, primary_key = True)
    vcluster_temp_id = Column(Integer, ForeignKey('vcluster_temps.id'))
    vm_temp_id = Column(Integer, ForeignKey('vm_temps.id'))
    count = Column(Integer)
    settings = Column(Text)
    ref_type = Column(String(255))

class VirtualClusterInstance(Base):
    __tablename__ = 'virtual_cluster_instances'

    id = Column(Integer, primary_key = True)
    name = Column(String(255))
    description = Column(String(255))
    vcluster_temp_id = Column(Integer, ForeignKey('vcluster_temps.id'))
    admin_role_id = Column(Integer)
    user_role_id = Column(Integer)
    created_at = Column(DateTime, default=datetime.utcnow)
    user_id = Column(Integer, ForeignKey('users.id'))
    status = Column(String(255))
    deployment = Column(Boolean)
    uuid = Column(String(255))
    worknode_count = Column(Integer)
    job_id = Column(Integer, ForeignKey('jobs.id'))

class VirtualMachineContainer(Base):
    __tablename__ = 'virtual_machine_containers'

    id = Column(Integer, primary_key = True)
    hostname = Column(String(255))
    address = Column(String(255))
    port = Column(Integer)
    status = Column(String(255))
    owner_id = Column(Integer)
    cpu_type = Column(String(255))
    cpu_num = Column(String(255))
    cpu_usage = Column(String(255))
    mem_total = Column(String(255))
    mem_free = Column(String(255))
    disk_device = Column(String(255))
    disk_total = Column(String(255))
    disk_free = Column(String(255))
    net_ifname = Column(String(255))
    net_tx = Column(String(255))
    net_rx = Column(String(255))
    vm_name = Column(String(255))
    vm_state = Column(String(255))
    vcpu_usage = Column(String(255))
    vmem_total = Column(String(255))
    vmem_free = Column(String(255))
    vdisk_read = Column(String(255))
    vdisk_write = Column(String(255))
    vif_tx = Column(String(255))
    vif_rx = Column(String(255))
    field_metadata = Column('metadata', String(255))
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    vm_uuid = Column(Text)
    capability = Column(String(255))
    vnc_port = Column(String(255))

class VirtualMachineInstance(Base):
    __tablename__ = 'virtual_machine_instances'

    id = Column(Integer, primary_key = True)
    uuid = Column(String(255))
    hostname = Column(String(255))
    vm_temp_id = Column(Integer, ForeignKey('vm_temps.id'))
    virtual_machine_container_id = Column(Integer, ForeignKey('virtual_machine_containers.id'))
    status = Column(String(255))
    virtual_cluster_instance_id = Column(Integer, ForeignKey('virtual_cluster_instances.id'))
    created_at = Column(DateTime, default=datetime.utcnow)
    cpu_usage = Column(String(255))
    mem_total = Column(String(255))
    mem_free = Column(String(255))
    disk_total = Column(String(255))
    disk_free = Column(String(255))
    disk_read = Column(String(255))
    disk_write = Column(String(255))
    vif_tx = Column(String(255))
    vif_rx = Column(String(255))
    vnc_port = Column(String(255))
    vlab_instance_id = Column(Integer, ForeignKey('vlab_instances.id'))
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    settings = Column(Text)
    target_vmc_id = Column(Integer, ForeignKey('virtual_machine_containers.id'))
    mig_info = Column(String(255))
    node_index = Column(Integer)
    job_id = Column(Integer, ForeignKey('jobs.id'))
    capture_nics = Column(String(255))
    capture_expr = Column(String(255))
    capabilities = Column(String(255))

class VlabInstance(Base):
    __tablename__ = 'vlab_instances'

    id = Column(Integer, primary_key = True)
    vlab_temp_id = Column(Integer, ForeignKey('vlab_temps.id'))
    created_at = Column(DateTime, default=datetime.utcnow)
    user_id = Column(Integer, ForeignKey('users.id'))
    status = Column(String(255))
    uuid = Column(String(255))
    name = Column(String(255))
    description = Column(String(255))
    deployment = Column(Boolean)
    job_id = Column(Integer, ForeignKey('jobs.id'))

class VlabTemp(Base):
    __tablename__ = 'vlab_temps'

    id = Column(Integer, primary_key = True)
    name = Column(String(255))
    description = Column(String(255))
    user_id = Column(Integer, ForeignKey('users.id'))
    vswitch_config = Column(Text)
    xml_content = Column(Text)
    uuid = Column(String(255))

class VlabTempsVmTemp(Base):
    __tablename__ = 'vlab_temps_vm_temps'

    id = Column(Integer, primary_key = True)
    vlab_temp_id = Column(Integer, ForeignKey('vlab_temps.id'))
    vm_temp_id = Column(Integer, ForeignKey('vm_temps.id'))
    settings = Column(Text)

class VmTemp(Base):
    __tablename__ = 'vm_temps'

    id = Column(Integer, primary_key = True)
    uuid = Column(String(255))
    name = Column(String(255))
    os_type = Column(String(255))
    distribution = Column(String(255))
    kernel = Column(String(255))
    capabilities = Column(String(255))
    repository = Column(String(255))
    deploy_method = Column(String(255))
    deploy_url = Column(String(255))
    prefer_settings = Column(String(255))
    node_type = Column(String(255))
    url = Column(String(255))
    description = Column(String(255))
    deploy_cowdir = Column(String(255))

class Vswitch(Base):
    __tablename__ = 'vswitches'

    id = Column(Integer, primary_key = True)
    uuid = Column(String(255))
    status = Column(String(255))
    virtual_machine_container_id = Column(Integer, ForeignKey('virtual_machine_containers.id'))
    virtual_cluster_instance_id = Column(Integer, ForeignKey('virtual_cluster_instances.id'))
    vlab_instance_id = Column(Integer, ForeignKey('vlab_instances.id'))
    created_at = Column(DateTime, default=datetime.utcnow)
    config_id = Column(Integer)
    connect_type = Column(String(255))
    ip = Column(String(255))
    internet_access = Column(Boolean)
    netmask = Column(String(255))
    gateway_virtual_machine_container_id = Column(Integer, ForeignKey('virtual_machine_containers.id'))
    port = Column(Integer)

class Snapshot(Base):
    __tablename__ = 'snapshots'
     
    id = Column(Integer, primary_key = True)
    uuid = Column(String(255))
    virtual_cluster_instance_id = Column(Integer, ForeignKey('virtual_cluster_instances.id'))
    vlab_instance_id = Column(Integer, ForeignKey('vlab_instances.id'))
    description = Column(String(255))
    created_at = Column(DateTime, default=datetime.utcnow)
 
class Dnet(Base):
    __tablename__ = 'vgateway_dnets'
 
    id = Column(Integer, primary_key = True)
    virtual_machine_instance_id = Column(Integer, ForeignKey('virtual_machine_instances.id'))
    vswitch_id = Column(Integer, ForeignKey('vswitches.id'))
    internal_ip = Column(String(255))
    internal_port = Column(Integer)
    protocol = Column(String(255))
    external_ip = Column(String(255))
    external_port = Column(Integer)

class WebFarm(Base):
    __tablename__ = 'webfarms'

    id = Column(Integer, primary_key = True)
    virtual_cluster_instance_id = Column(Integer, ForeignKey('virtual_cluster_instances.id'))
    scale = Column(Boolean)
    current_load = Column(String(255))
    current_node = Column(Integer)
    online = Column(String(255))
    standby = Column(String(255))
    reserved = Column(String(255))
    address = Column(String(255))
